package com.health.home.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.health.home.domain.po.TmedicinePo;
import com.health.home.domain.po.TmedicineTypePo;
import com.health.home.domain.vo.TmedicineTypeVo;
import com.health.home.domain.vo.TmedicineVo;
import com.health.home.service.TmedicineService;
import com.health.home.service.TmedicineTypeService;
import com.health.home.util.PageResult;
import com.health.home.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xpz
 * @date 2024/12/11 22:32
 */
@RestController
@RequestMapping("/pharmaceuticals")
public class TmedicineController {

    @Autowired
    private TmedicineService tmedicineService;

    @Autowired
    private TmedicineTypeService tmedicineTypeService;
    /**
     * 查询药品类型关联药品信息 对接患者前端
     */
    @PostMapping("/pharmaceuticalsList")
    public Result<List<TmedicinePo>> temdicineList(){
        List<TmedicinePo> list = tmedicineService.temdicineList();
        return Result.success(list);
    }

    /**
     * 单个查询药品信息 对接后端管理前端
     */
     @PostMapping("/pharmaceuticalsLists")
     public Result<PageResult<TmedicinePo>>  pharmaceuticalsLists(@ModelAttribute TmedicineVo tmedicineVo){
         PageHelper.startPage(tmedicineVo.getPageNum(), tmedicineVo.getPageSize());
         List<TmedicinePo> list = tmedicineService.pharmaceuticalsLists(tmedicineVo);
         PageInfo<TmedicinePo> info = new PageInfo<>(list);
         return PageResult.toResult(info.getTotal(),info.getList());
     }

    /**
     * 新增药品信息
     */
    @PostMapping("/pharmaceuticalsAdd")
    public Result pharmaceuticalsAdd(@ModelAttribute TmedicinePo tmedicinePo){
        return tmedicineService.pharmaceuticalsAdd(tmedicinePo);
    }

    /**
     * 编辑药品信息
     */
    @PostMapping("/pharmaceuticalsUpdate")
    public Result pharmaceuticalsUpdate(@ModelAttribute TmedicinePo tmedicinePo){
        return tmedicineService.pharmaceuticalsUpdate(tmedicinePo);
    }

    /**
     * 删除药品信息
     */

    @PostMapping("/pharmaceuticalsDel")
    public Result pharmaceuticalsDel(@RequestParam("id") Integer id){
         tmedicineService.pharmaceuticalsDel(id);
         return Result.success();
    }

    /**
     * 药品类型查询
     */
     @PostMapping("/tmedicineTypeList")
     public List<TmedicineTypePo> tmedicineTypeList(@ModelAttribute TmedicineTypeVo tmedicineTypeVo){
         return  tmedicineTypeService.lists(tmedicineTypeVo);
     }

    /**
     * 删除药品类型
     */
    @PostMapping("/tmedicineTypeDel")
     public Result tmedicineTypeDel(@RequestParam("id") Integer id){
         tmedicineTypeService.removeById(id);
         return Result.success();
     }

    /**
     * 新增药品类型
     */
    @PostMapping("/tmedicineTypeAdd")
     public Result tmedicineTypeAdd(@ModelAttribute TmedicineTypePo tmedicineTypePo){

        try {
            // 调用服务层保存方法，内部会进行唯一性验证
            tmedicineTypeService.saveMedicineType(tmedicineTypePo);
            return Result.success();
        } catch (RuntimeException  e) {
            // 如果发生唯一性冲突，捕获异常并返回错误信息
           return Result.error(e.getMessage());

        }

     }
    /**
     * 编辑药品类型
     */
    @PostMapping("/tmedicineTypeUpdate")
    public Result tmedicineTypeUpdate(@ModelAttribute TmedicineTypePo tmedicineTypePo){
         tmedicineTypeService.updateById(tmedicineTypePo);
         return Result.success();
     }
}
